package sync;

public class ThreadSync {
	public static void main(String[] args) {
		  int[] in_out=new int[2];
		  Producer p=new Producer(in_out);
		  Consumer c=new Consumer(in_out);
		  p.start();
		  c.start();
	}

}
class Consumer extends Thread{

	int[] in_out;
    public Consumer(int[] in_out)
    {
    	this.in_out=in_out;
    }
	public void run()
	{
	  
	  try {
	    while(true)
	    {	 
           synchronized(in_out)
           {
 	    	 while(in_out[0]-in_out[1]<=0)
	    		  in_out.wait();   
	    	 this.in_out[1]++;
	    	 System.out.println(in_out[0]-in_out[1]);
	    	 in_out.notifyAll();		 
          }
		   Thread.sleep(250);
	    }
	  } catch (Exception e) {
			e.printStackTrace();
		}
	}
}
class Producer extends Thread{


	int[] in_out;
    public Producer(int[] in_out)
    {
    	
    	this.in_out=in_out;
    }
	public void run()
	{
  
	  try {
	    while(true)
	    {
	       synchronized(in_out)
	       {
	    	  while(in_out[0]-in_out[1]>=5)
	    		  in_out.wait();
		      this.in_out[0]++;
		      System.out.println(in_out[0]-in_out[1]);
		      in_out.notifyAll();		      
	       }
		   Thread.sleep(200);
	    }
	  } catch (Exception e) {
			e.printStackTrace();
		}
	}
}
